package de.gwtprime.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.presenter.VoteControlPresenter;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.shared.VoteType;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;
import de.gwtprime.shared.requestFactory.User2Proxy;

public class ArticleSectionControlWidget extends Composite implements ArticleSectionControlWidgetInterface{

	ArticleSectionWidgetControlUiBinder uiBinder = GWT
			.create(ArticleSectionWidgetControlUiBinder.class);

	interface ArticleSectionWidgetControlUiBinder extends
			UiBinder<Widget, ArticleSectionControlWidget> {
	}
	
	private VoteControlPresenter presenter;

	@UiField Label voteValueBtn;
	@UiField TooltipButton voteUpBtn;
	@UiField TooltipButton voteDownBtn;
	@UiField TooltipButton voteCompleteBtn;
	
	/**
	 * Keep the vote state on the UI
	 */
	VoteType voteType; // TODO Remove
	
	
	private boolean showCompleteVoteBtn = true;
	
	ClientFactory clientFactory;
	public ArticleSectionControlWidget(ClientFactory clientFactory){
		
		
		initWidget(uiBinder.createAndBindUi(this));
		this.clientFactory = clientFactory;
		
		voteUpBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				// If the button is disabled -> show a message
				// Button can be disabled:
				// * User already voted up
				// * User has no right to vote up
				if(!voteUpBtn.checkEnabledStateAndShowMessage(event)){
					return;
				}
				
				presenter.voteUp();
			}
		});
		
		voteDownBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				// If the button is disabled -> show a message
				// Button can be disabled:
				// * User already voted down
				// * User has no right to vote down
				if(!voteDownBtn.checkEnabledStateAndShowMessage(event)){
					return;
				}
				
				presenter.voteDown();
			}
		});
		
		
		voteCompleteBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				// If the button is disabled -> show a message
				// Button can be disabled:
				// * User already voted down
				// * User has no right to vote down
				if(!voteCompleteBtn.checkEnabledStateAndShowMessage(event)){
					return;
				}
				
				presenter.onVoteComplete();
			}
		});
		
	}
	
	public ArticleSectionControlWidget(VoteControlPresenter presenter){
		this.presenter = presenter;
	}

	public ArticleSectionControlWidget(final ArticleSectionProxy articleSection, final ArticleProxy article,
			final ClientFactory clientFactory) {
		
		
	}

	public VoteControlPresenter getPresenter() {
		return presenter;
	}

	public void initWidget(final VoteControlPresenter presenter) {
		this.presenter = presenter;
		

		presenter.initializeListeners();
		
	}
	
	public void setPresenter(VoteControlPresenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setVoteValue(Integer voteValue) {
		voteValueBtn.setText(String.valueOf(voteValue));
		
	}

	@Override
	public void enableVoteUpBtn(Boolean enable) {
		
		voteUpBtn.setEnabled(enable);
	}
	
	@Override
	public void disableVoteUpButtonWithMessage(Boolean enable, String message) {
		voteUpBtn.disableButtonWithMessage(enable, message);
	}
	
	@Override
	public void disableVoteDownButtonWithMessage(Boolean enable, String message) {
		voteDownBtn.disableButtonWithMessage(enable, message);
	}
	
	@Override
	public void disableVoteCompleteButtonWithMessage(Boolean enable, String message) {
		voteCompleteBtn.disableButtonWithMessage(enable, message);
	}
	
	@Override
	public void setVoteUpBtnActive(Boolean enable) {
		
		if(enable){
			voteUpBtn.addStyleName("active");
		}else{
			voteUpBtn.removeStyleName("active");
		}
	}

	@Override
	public void enableVoteDownBtn(Boolean enable) {
		voteDownBtn.setEnabled(enable);
	}
	
	@Override
	public void enableVoteDownBtnFromUser(PrimeUIElement uiElement, User2Proxy elementOwner) {
		voteDownBtn.enableFromUser(clientFactory, uiElement, elementOwner);
	}
	
	@Override
	public void enableVoteUpBtnFromUser(PrimeUIElement uiElement, User2Proxy elementOwner) {
		voteUpBtn.enableFromUser(clientFactory, uiElement, elementOwner);
	}
	
	
	@Override
	public void setVoteDownBtnActive(Boolean enable) {
		
		if(enable){
			voteDownBtn.addStyleName("active");
		}else{
			voteDownBtn.removeStyleName("active");
		}
	}

	@Override
	public void enableVoteCompleteBtn(Boolean enable) {
		voteCompleteBtn.setEnabled(enable);		
	}
	
	
	@Override
	public void enableVoteCompleteBtnFromUser(PrimeUIElement uiElement, User2Proxy elementOwner) {
		voteCompleteBtn.enableFromUser(clientFactory, uiElement, elementOwner);
	}

	@Override
	public void setVoteCompleteBtnActive(Boolean enable) {
		if(enable){
			voteCompleteBtn.addStyleName("active");
		}else{
			voteCompleteBtn.removeStyleName("active");
		}		
	}

	public boolean isShowCompleteVoteBtn() {
		return showCompleteVoteBtn;
	}

	public void setShowCompleteVoteBtn(boolean showCompleteVoteBtn) {
		this.showCompleteVoteBtn = showCompleteVoteBtn;
		voteCompleteBtn.setVisible(showCompleteVoteBtn);
	}

}
